﻿
$(document).ready(function () {
    //    $("#txt_frdate").datepicker({ dateFormat: 'dd/mm/yy' });
    //    $("#txt_todate").datepicker({ dateFormat: 'dd/mm/yy' });
    //    $("#txt_DateRecorded").datepicker({ dateFormat: 'dd/mm/yy' });
    //$("#plistdateto").datepicker({ dateFormat: 'dd/mm/yy' });
});

function checkNuber(num) {
    if (num != "") {
        if (isNaN(num) == true) {
            alert("Yêu cầu nhập số!");
            return false;
        }
        return true;
    }
    return true;
}

function fireParentForm(LevelLocationType, WebpartId, PermissionCode) {

}

function ClientStatus() {
    alert("Chuyên mục này đang xây dựng!");
    return;
}

function ClientShowAddnewFundingsForm() {
    Url = Portal.SODC.IDIS.ListPreFundings.GetShowAddNewsForm().value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    retValue = window.open(Url, "_blank");
}

function ClientShowEditFundingsForm(RecordId) {
    Url = Portal.SODC.IDIS.ListPreFundings.GetShowUpdateForm(RecordId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    retValue = window.open(Url, "_blank");
}


function ClientShowDetailFundings(RecordId) {
    Url = Portal.SODC.IDIS.ListPreFundings.GetShowDetailForm(RecordId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    retValue = window.open(Url, "_blank");
}


function ClientUpdateFundings(RecordId) {

    var LocationId = $("#_locationsel").val();
    if (typeof (LocationId) == 'undefined' || LocationId == null) {
        var LocationId_Curent = $("#LocationId_Curent").val();
        LocationId = LocationId_Curent;
    }
    var daterecord = document.getElementById("drpDaytxt_DateRecorded").options[document.getElementById("drpDaytxt_DateRecorded").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_DateRecorded").options[document.getElementById("drpMonthtxt_DateRecorded").selectedIndex].value + '/' + document.getElementById("drpYeartxt_DateRecorded").options[document.getElementById("drpYeartxt_DateRecorded").selectedIndex].value;
    if (daterecord == "//") {
        alert("Yêu cầu nhập ngày tháng!!!");
        return;
    } else {
        if (!CheckValue()) {
            return;
        }   
        var tw = document.getElementById("txt_tw").value;
        var dp = document.getElementById("txt_diaphuong").value;
        var nuocngoai = document.getElementById("txt_nuocngoai").value;
        var trongnuoc = document.getElementById("txt_trongnuoc").value;
        var khac = document.getElementById("txt_khac").value;
        var sudung = document.getElementById("txt_sudung").value;
        RetValue = Portal.SODC.IDIS.EditPreFundings.ServerSideUpdateFundings(daterecord, tw, dp, nuocngoai, trongnuoc, khac, sudung, RecordId, LocationId).value;
        if (RetValue.indexOf("ERROR") != -1) {
            alert(RetValue.replace("ERROR: ", ""));
            return;
        } else {
            alert(RetValue);
        }

    }
}

function selectLevelTypeforUpdateFundings() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.EditPreFundings.ServerSideRenderLocationSelect(ltype).value);
}

function selectLevelTypeforInsertFundings() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.AddNewPreFundings.ServerSideRenderLocationSelect(ltype).value);
}

function selectLevelTypeforSearchFundings() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.ListPreFundings.ServerSideRenderLocationSelect(ltype).value);
}

function selectLevelTypeforFillter() {
    var ltype = $("#selectlevelFillter").val();
    $("#selectlevelZone").html(Portal.SODC.IDIS.ListPreFundings.ServerSideRenderLocationFillter(ltype).value);
}

function ClientDoAddFundings() {

    var locationId = $("#_locationsel").val();
    if (locationId == null || locationId == undefined) {
        alert("Bạn phải chọn cấp - địa phương để nhập dữ liệu !")
        return;
    } else if (locationId.length == 0 || locationId == 0) {
        alert("Bạn phải chọn cấp - địa phương để nhập dữ liệu !")
        return;
    }

    var daterecord = document.getElementById("drpDaytxt_DateRecorded").options[document.getElementById("drpDaytxt_DateRecorded").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_DateRecorded").options[document.getElementById("drpMonthtxt_DateRecorded").selectedIndex].value + '/' + document.getElementById("drpYeartxt_DateRecorded").options[document.getElementById("drpYeartxt_DateRecorded").selectedIndex].value;

    if (daterecord == "//") {
        alert("Yêu cầu nhập ngày tháng!!!");
        return;
    } else {
        if (!CheckValue()) {
            return;
    }   
        var tw = document.getElementById("txt_tw").value;       
        var dp = document.getElementById("txt_diaphuong").value;
        var nuocngoai = document.getElementById("txt_nuocngoai").value;
        var trongnuoc = document.getElementById("txt_trongnuoc").value;
        var khac = document.getElementById("txt_khac").value;
        var sudung = document.getElementById("txt_sudung").value;        
        RetValue = Portal.SODC.IDIS.AddNewPreFundings.ServerSideAddNewFundings(daterecord, tw, dp, nuocngoai, trongnuoc, khac, sudung, locationId).value;
        if (RetValue.indexOf("ERROR") != -1) {
            alert(RetValue.replace("ERROR: ", ""));
            return;
        } else {
            alert(RetValue);
            ResetAllFieldFundings();
        }

    }
}


function CheckValue() {
   
    var textBoxes = $("input[type='text']");
    for (var i = 0; i < textBoxes.length; i++) {
        value = textBoxes[i].value;
        if (value != '' && value != null) {
            if (!IsNumeric(value)) {
                alert("Yêu cầu nhập số!");
                textBoxes[i].focus();
                return false;
            }
        } else {
            textBoxes[i].value = 0 ;
        }
    }
    return true; 
}


function IsNumeric(sText) {
    var ValidChars = "0123456789,";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function isValidDate(sText) {
    var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
    return reDate.test(sText);
}


function ResetAllFieldFundings() {
    var textBoxes = $("input[type='text']");
    for (var i = 0; i < textBoxes.length; i++) {
        textBoxes[i].value = 0;
    }
}

/*function checkdate(date) {
var validformat = /^\d{2}\/\d{2}\/\d{4}$/;
var returnval = false;
if (!validformat.test(date)) {
alert("Nhập sai định dạng ngày.");
}
else {
var monthfield = date.split("/")[1];
var dayfield = date.split("/")[0];
var yearfield = date.split("/")[2];
var dayobj = new Date(yearfield, monthfield - 1, dayfield);
if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield)) {
alert("Nhập theo định dạng ngày/tháng/năm.");
}
else {
returnval = true;
}
}
return returnval;
}*/


function ClientDoSimpleSearchFundings() {
    var frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    var toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;

    var LocationId = $("#_locationsel").val();

    if (frDate == "//") {
        frDate = null;
    }
    if (toDate == "//") {
        toDate = null;
    }

    elem = document.getElementById("Notkey");
    result = document.getElementById("ContentSearchResult");
    if (LocationId) {
        document.getElementById("divProcessingSearchResult").innerHTML = "";
        ps = null;
        var retVal = Portal.SODC.IDIS.ListPreFundings.GetResult('', LocationId, frDate, toDate, 0, "JumpPageSearchResultPreFundings", ps).value;
        if (retVal.indexOf("ERROR") != -1) {
            alert(retVal.replace("ERROR: ", ""));
            return;
        }
        result.innerHTML = retVal;

    } else {
        alert("Chưa xác định được địa phương !!!");
        result.innerHTML = "";
    }
}

function JumpPageSearchResultPreFundings(wpId, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    document.getElementById("divProcessingSearchResult").innerHTML = Portal.SODC.IDIS.ListPreFundings.GetProcessingText().value;
    setTimeout("RealJumpPageSearchResultPreFundings('" + wpId + "','" + MaxItems + "', 20, 10, '" + CurrentPage + "', 'RealJumpPageSearchResultPreFundings')", 10);
}
function RealJumpPageSearchResultPreFundings(Wp, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    PageIndex = CurrentPage;
    LocationId = document.getElementById("txt_LocationId").value;
    var frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    var toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;
    ps = null;
    retValue = Portal.SODC.IDIS.ListPreFundings.GetResult(Wp, LocationId, frDate, toDate, CurrentPage, JumpJavaScript, ps).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue);
        return;
    }
    document.getElementById("divProcessingSearchResult").innerHTML = "";
    document.getElementById("ContentSearchResult").innerHTML = retValue;
}

// Delete InterCoop Info
function DeleteFundingsInfo(RecordId, LocationId) {
    Msg = Portal.SODC.IDIS.ListPreFundings.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    retVal = Portal.SODC.IDIS.ListPreFundings.ServerSideDeleteFundings(RecordId, LocationId).value;
    if (retVal.indexOf("ERROR") != -1) {
        alert(retVal);
        return;
    }
    alert(retVal);
    ClientDoSearchByLocation();
}

function ClientSideExportDataFundings() {
    var frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    var toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;
    var LocationId = $("#_locationsel").val();
    if (frDate == "//") {
        frDate = null;
    }
    if (toDate == "//") {
        toDate = null;
    }
    if (LocationId) {
        Ret = Portal.SODC.IDIS.ListPreFundings.ServerExportDataFundings(LocationId, frDate, toDate).value;
        document.getElementById("divProcessingSearchResult").innerHTML = "Hệ thống đang xử lý";
        if (Ret.indexOf("ERROR") != -1) {
            alert(Ret.replace("ERROR: ", ""));
            document.getElementById("divProcessingSearchResult").innerHTML = "";
            return;
        } else {
            document.getElementById("divProcessingSearchResult").innerHTML = "";
            window.open(Ret, "_blank");
        }
    } else {
        alert("Chưa xác định được địa phương !!!");
        return;
    }
}

function ShowValue(Id) {
    var value = document.getElementById(Id).value
    if (value != "") {
        if (!IsNumeric(value)) {
            alert("Yêu cầu nhập số!");
            return;
        } else {
            document.getElementById("lb_" + Id).innerHTML = Portal.SODC.IDIS.Dictionarys.ReformatNumberVie(value).value;
        }
    } else {
        alert("Bạn phải nhập giá trị để xem dữ liệu!");
        return;
    }
}